package LeetCode.leetcode19;

import java.util.HashMap;

class Solution {
    public static void main(String[] args) {
        ListNode head = new ListNode();
        head.val = 1;
        head.next = null;
        System.out.println(new Solution().removeNthFromEnd(head, 1));
    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pointer = head;
        HashMap<Integer, ListNode> map = new HashMap<>();
        int size = 0;
        ListNode preHead = new ListNode();
        preHead.next = head;
        map.put(0, preHead);
        while (pointer != null) {
            size++;
            map.put(size, pointer);
            pointer = pointer.next;
        }
        ListNode pre = map.get(size - n);
        pre.next = pre.next.next;
        return preHead.next;
    }
}